package cdc.cs.service;

import cdc.cs.entity.BusinessPartnersAddressVO;
import cdc.cs.entity.BusinessPartnersVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName: TimerService
 * @Description: TODO
 * @Author: kevin
 * @Date: 2020/5/29 14:18
 * @Version: 1.0
 */
@Service
@Slf4j
public class TimerService {
    @Autowired
    ODataPartnerService partnerService;
    @Autowired
    ODataPartnersAddressService addressService;
    @Autowired
    ODataPartnersPhoneService phoneService;
    @Autowired
    ODataPartnersBankService bankService;
    @Autowired
    ODataPartnersRoleService roleService;

    /**
     * 更新、同步数据
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void updatePartnerData() {
        /** 获取所有合作伙伴数据 */
        Integer all = partnerService.partnerGetAll();
        /** 每页5条数据， 总共有多少页 */
        int count = (int) Math.ceil(all/5.0);
        System.out.println("---------一共" + count + "页-------");
        for (int i = 1; i <= count; i++) {
            System.out.println("---------第" + i + "页-------");
            List<BusinessPartnersVO> businessPartnersVOS = partnerService.partnerPagination(i);
            if (businessPartnersVOS != null && businessPartnersVOS.size() > 0) {
                for (BusinessPartnersVO partner : businessPartnersVOS) {
                    boolean exist = partnerService.queryTarget(partner);
                    if (!exist) {
                        /** 保存合作伙伴 */
                        boolean partnerFlag = partnerService.savePartner(partner);
                        /** 保存地址 */
                        boolean addressFlag = addressService.savePartnerAddress(partner.getToAddress());
                        BusinessPartnersAddressVO addressVO = addressService.queryOneAddress(partner.getToAddress());
                        /** 保存电话 */
                        if (addressVO != null && addressVO.getToPhone() != null) {
                            boolean phoneFlag = phoneService.savePartnerPhone(addressVO.getToPhone(), partner.getBusinessPartner());
                        }
                        /** 保存角色 */
                        boolean roleFlag = roleService.savePartnerRole(partner.getToRole());
                        /** 保存银行 */
                        boolean bankFlag = bankService.savePartnerBank(partner.getToBank());


                    } else {
                        continue;
                    }
                }
            }
        }
    }
}
